<?php

require_once(dirname(__FILE__).'/DB_New.php');
require_once(dirname(__FILE__).'/Note/List.php');
require_once(dirname(__FILE__).'/Schedule.php');
require_once(dirname(__FILE__).'/HimaShare.php');
require_once(dirname(__FILE__).'/Util.php');

class MyList {
    private $owner_id    = 0;
    public  $owner       = null;
    public  $friend_list = array();

    public function __construct ($owner_id) {
        $owner       = HimaShare::get_member($owner_id);
        $friend_list = HimaShare::get_all_friends($owner_id);

        $this->owner_id    = $owner_id;
        $this->owner       = $owner;
        $this->friend_list = $friend_list;
    }

    public function get_list_for_index ($start_date, $offset = 0, $days = 3) {
        $people_list = $this->friend_list;
        $people_list[$this->owner_id] = $this->owner;
        $id_list = array_keys($people_list);

        $db = new DB_Note('slv');
        $dates = $db->select_recent_days($id_list, $start_date, $offset, $days);
        sort($dates);

        $list = array();
        foreach ($dates as $date) {
            $temp_hash = array();

            $hima_list = Schedule::get_list($id_list, $date, $date);
            foreach ($hima_list as $hima) {
                $temp_hash[$hima['member_id']]['hima'] = $hima;
            }
            $note_list = Note_List::get_by_member($id_list, $date);
            foreach ($note_list as $note) {
                if ($note->is_public() || $note->member_id == $this->owner_id)
                    $temp_hash[$note->member_id]['note_list'][] = $note;
            }

            $entry = array();
            foreach ($temp_hash as $member_id => $schedule) {
                $entry[] = array(
                    'member_id'     => $member_id,
                    'member_info'   => $people_list[$member_id],
                    'hima'          => $schedule['hima'],
                    'note_list'     => $schedule['note_list'],
                );
            }

            $list[] = array(
                'date'  => $date,
                'stamp' => date_to_unix($date),
                'entry' => $entry,
            );
        }

        return $list;
    }

    public function get_list_for_calendar ($year, $month) {
    }

    public function get_list_of_date_self ($date) {
        $db = new DB_Note('slv');

        $hima_list = Schedule::get_list($this->owner_id, $date, $date);
        $note_list = Note_List::get_by_member($this->owner_id, $date);

        $entry = array(
            'member_id'   => $this->owner_id,
            'member_info' => $this->owner,
            'hima'        => array_pop($hima_list),
            'note_list'   => $note_list,
        );

        return $entry;
    }

    public function get_list_of_date_friends ($date) {
        $people_list = $this->friend_list;
        $id_list = array_keys($people_list);

        $db = new DB_Note('slv');

        $hima_list = Schedule::get_list($id_list, $date, $date);
        $temp_hash = array();
        foreach ($hima_list as $hima) {
            $temp_hash[$hima['member_id']]['hima'] = $hima;
        }
        $note_list = Note_List::get_by_member($id_list, $date);
        foreach ($note_list as $note) {
            if (!$note->is_public())
                continue;
            $temp_hash[$note->member_id]['note_list'][] = $note;
        }

        $entry = array();
        foreach ($temp_hash as $member_id => $schedule) {
            $entry[] = array(
                'member_id'   => $member_id,
                'member_info' => $people_list[$member_id],
                'hima'        => $schedule['hima'],
                'note_list'   => $schedule['note_list'],
            );
        }

        return $entry;
    }

    public function get_list_for_notes_self () {
    }

    public function get_list_for_notes_friends () {
    }
}
